﻿
#if !(UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX || UNITY_STANDALONE_OSX || STEAMWORKS_WIN || STEAMWORKS_LIN_OSX)
#define DISABLESTEAMWORKS
#endif

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MineFiledPanel : MonoBehaviour
{
    public GameObject m_Tips;
    public Text m_TimeL;
    public Text m_NameL;
    public Text m_DescriptionL;
    public SceneSwiter m_SceneSwiter;
    public Button m_EntranceTicketB;
    public Text m_EntranceTicketT;



    List<MineFiled> m_MineFileds = new List<MineFiled>();
    MineFiled CurrentMineFiled {
        get { return m_CurrentMineFiled; }
        set {
            if (null == value)
                return;
            m_CurrentMineFiled = value;
            m_NameL.text = value.Name;
            m_DescriptionL.text = value.Description;

            m_EntranceTicketT.text = "支付入场费：$" + m_CurrentMineFiled.m_EntranceTicketPrice;
            m_EntranceTicketB.gameObject.SetActive(true);
        }
    }
    MineFiled m_CurrentMineFiled=null;

    bool m_IsStart = false;
    float m_TimeCount = 0;
    float m_TimeCountMax = 1;

    float m_AccumulativeTime = 0;

    private void Awake()
    {
        m_MineFileds.Add(new MineFiled_23());
        CurrentMineFiled = m_MineFileds[0];

    }
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (!m_IsStart)
            return;

        m_TimeCount += Time.deltaTime;
        if (m_TimeCount < m_TimeCountMax)
            return;
        m_TimeCount = 0;

        var item = m_CurrentMineFiled.Dig();
        if(item != null)
        {

            GameObject sound = Instantiate(Resources.Load<GameObject>("Perfabs/Audio/Ding"));
            if (sound && sound.GetComponent<AudioSource>())
            {
                sound.GetComponent<AudioSource>().Play();
                Destroy(sound, 7);
            }

#if !DISABLESTEAMWORKS
            if (UtilsGame.IsSteamMG())
            {
                int digNumber = 0;
                if(Steamworks.SteamUserStats.GetStat("dig_count", out digNumber))
                {
                    Steamworks.SteamUserStats.SetStat("dig_count", (++digNumber));
                    if (digNumber >= 100)
                        Steamworks.SteamUserStats.SetAchievement("HIGH_MINER");
                    if (digNumber >= 1000)
                        Steamworks.SteamUserStats.SetAchievement("PRO_MINER");
                    if (digNumber >= 10000)
                        Steamworks.SteamUserStats.SetAchievement("NIU_BI_MINER");
                }
                Steamworks.SteamUserStats.StoreStats();
            }
#endif
            Player.Instance.addItem(item.m_Type);
            Player.Instance.save();
        }
        m_AccumulativeTime++;
        m_TimeL.text = m_AccumulativeTime.ToString() + "s";
    }

    public void OnIntoMineFild()
    {
        if (!UtilsGame.CheckSelectShip())
            return;
        m_SceneSwiter.switchScene(CurrentMineFiled.m_SceneName);
    }

    public void OnStartDig()
    {
        m_AccumulativeTime = 0;
        m_IsStart = true;
        m_Tips.SetActive(true);
    }
    public void OnStopDig()
    {
        m_IsStart = false;
        m_Tips.SetActive(false);
    }

    public void OnEntranceTicketClicked()
    {
        if (!UtilsGame.CheckSelectShip())
            return;
        if (Player.Instance.Money < m_CurrentMineFiled.m_EntranceTicketPrice)
        {
            MessageShow.Instance.Messaage("你钱不够~");
            return;
        }
        Player.Instance.Money -= m_CurrentMineFiled.m_EntranceTicketPrice;
        m_EntranceTicketB.gameObject.SetActive(false);
    }
}
